package br.com.inner.modelo;

import java.math.BigDecimal;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Query;
import javax.persistence.Table;
import javax.persistence.TableGenerator;

import br.com.inner.custom.CustomException;
import br.com.inner.custom.JPAManager;

@Entity
@Table(name="Categoria")
@NamedQueries({
	@NamedQuery(name="CategoriasPK",
			query="SELECT MAX(c.cdCategoria) FROM Categoria c"),
	@NamedQuery(name="Categoria",
			query="SELECT c FROM Categoria c WHERE c.cdCategoria = :cdCategoria"),
	@NamedQuery(name="CategoriasAbertas",
			query="SELECT c FROM Categoria c WHERE c.statusCategoria like :status"),
	@NamedQuery(name="CategoriasPorDescricao",
			query="SELECT c FROM Categoria c WHERE upper(c.nmCategoria) like :nmCategoria")
})
public class Categoria{

	@Id
	@GeneratedValue(generator="CategoriaTab")
	@TableGenerator(name="CategoriaTab", table="TABLE_GEN",
            pkColumnName="ID_NAME", valueColumnName="ID_VAL",
            pkColumnValue="CATEGORIA_GEN", allocationSize=1)
	@Column(name="cdCategoria", length=3)
	private BigDecimal cdCategoria;
	
	@Column(name="nmCategoria", length=20)
	private String nmCategoria;
	
	@Column(name="status", length=1)
	private String statusCategoria;
	
	@OneToMany(mappedBy="categoria")
	List<Empresa> listEmpresa;
	
	/*public void addUsuario(Usuario usuario){
		if(listUsuario == null){
			listUsuario = new ArrayList<Usuario>();
		}
		listUsuario.add(usuario);
	}*/
	
	public Categoria() {
		super();
	}
	
	public Categoria(BigDecimal cd){
		this.cdCategoria = cd;
	}
	
	public Categoria(BigDecimal cd, String nm){
		this.cdCategoria = cd;
		this.nmCategoria = nm;
	}
	
	public Categoria(BigDecimal cd, String nm, String st){
		this.cdCategoria = cd;
		this.nmCategoria = nm;
		this.statusCategoria = st;
	}
	
	/**
	 * @return the cdCategoria
	 */
	public BigDecimal getCdCategoria() {
		return cdCategoria;
	}
	/**
	 * @param cdCategoria the cdCategoria to set
	 */
	public void setCdCategoria(BigDecimal cdCategoria) {
		this.cdCategoria = cdCategoria;
	}
	/**
	 * @return the nmCategoria
	 */
	public String getNmCategoria() {
		return nmCategoria;
	}
	/**
	 * @param nmCategoria the nmCategoria to set
	 */
	public void setNmCategoria(String nmCategoria) {
		this.nmCategoria = nmCategoria;
	}
	/**
	 * @return the statusCategoria
	 */
	public String getStatusCategoria() {
		return statusCategoria;
	}
	/**
	 * @param statusCategoria the statusCategoria to set
	 */
	public void setStatusCategoria(String statusCategoria) {
		this.statusCategoria = statusCategoria;
	}
	
	public void generatePK() throws CustomException {
		System.out.println("Entrou no generatePK");
		Query query = null;
		BigDecimal result = null;
		System.out.println("Setou a query eo result == null");
		EntityManager manager = JPAManager.getEntityManager();
		System.out.println("Pegou a entidade");
		query = manager.createNamedQuery("CategoriasPK");
		System.out.println("Criou a NamedQuery: CategoriasPK");
		result = (BigDecimal)query.getSingleResult();
		System.out.println("Recebeu da query um result = "+result);
		if (result == null) {
			System.out.println("Entrou se o result == 1");
			result = new BigDecimal(1);
			System.out.println("Ficando result = "+result);
		}else{
			System.out.println("Entrou se o result <> 1");
			result = result.add(new BigDecimal(1));
			//result += new Integer(1);
			System.out.println("Ficando result = "+result);
		}
		System.out.println("Result: "+result);
		this.setPk(result);
	}
	
	public Object getPk() {
		return this.getCdCategoria();
	}
	
	public void setPk(Object newId) {
		// TODO Auto-generated method stub
	}

	public void setPk(BigDecimal _cdCategoria) {
		this.cdCategoria = _cdCategoria;
	}

	public int hashCode() {
		int hash = 0;
		hash += (this.cdCategoria != null ? this.cdCategoria.hashCode() : 0);
		return hash;
	}

	public boolean equals(Object object) {
		if (object == null || !this.getClass().equals(object.getClass())) {
			return false;
		}
		Categoria other = (Categoria) object;
		if (this.cdCategoria != other.cdCategoria
				&& (this.cdCategoria == null || !this.cdCategoria.equals(other.cdCategoria)))
			return false;
		return true;
	}
}
